package com.qf.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.springcloud.client.ProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RefreshScope
public class TestConroller {

    @Value("${consumer.version:v0.0.0}")
    private String consumerVersion;

    @GetMapping("/consumer/test3")
    public String test3(){
        return consumerVersion;
    }






































    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProviderClient providerClient;

    /*@Resource
    private EurekaClient eurekaClient;*/

    @GetMapping("/consumer/test")
    @HystrixCommand(fallbackMethod = "testFallback",commandProperties = {
            @HystrixProperty(name="execution.isolation.strategy",value = "SEMAPHORE"),
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
            @HystrixProperty(name="execution.timeout.enabled",value = "true"),
            @HystrixProperty(name="circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "5"),
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "20"),
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "60000")
    })
    public String test(Integer id) throws InterruptedException {
        /*
        //1. 基于服务的名称获取服务信息
        InstanceInfo provider = eurekaClient.getNextServerFromEureka("PROVIDER", false);
        String ipAddr = provider.getIPAddr();
        int port = provider.getPort();
        String result = restTemplate.getForObject("http://"+ipAddr+":"+port+"/provider/test", String.class);
        */

        /*
        // Ribbon整合后，直接编写服务名作为host，port
        String result = restTemplate.getForObject("http://PROVIDER/provider/test", String.class);
        */

        System.out.println(Thread.currentThread().getName());

        if(id == 1){
            throw new RuntimeException("嘿嘿嘿！");
        }else if (id == 2){
            Thread.sleep(3000);
        }

        // 调用了Provider服务……
        System.out.println(Thread.currentThread().getName());
        String result = providerClient.test();
        return "Hello Consumer Test And " + result;
    }


    public String testFallback(Integer id){
        return "Consumer的Test方法，崩了！";
    }


    @GetMapping("/consumer/test2")
    public String test2(){
        System.out.println(Thread.currentThread().getName());
        return "嘿嘿 =================> test2";
    }
}
